import { Entity, PrimaryGeneratedColumn, Column, BeforeInsert, BeforeUpdate } from 'typeorm';
import * as crypto from 'crypto';

@Entity('user')
export class UserEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userName: string;

  @Column()
  password: string;

  // 姓名
  @Column({ default: '' })
  name: string;

  @Column({ default: '' })
  phoneNum: string;

  @Column()
  email: string;

  // 头像
  @Column({ default: '' })
  logo: string;

  // 备注
  @Column({ default: '' })
  comments: string;

  // 用户组
  @Column({ default: '' })
  group: string;

  // 是否有效
  @Column({ default: true })
  enable: boolean;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  created: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  updated: Date;

  @BeforeUpdate()
  updateTimestamp() {
    this.updated = new Date();
  }

  @BeforeInsert()
  hashPassword() {
    this.password = crypto.createHmac('sha256', this.password).digest('hex');
  }
}
